//
//  BVSearchShopsCell.m
//  BuyVegetables1
//
//  Created by 叶思超 on 15/10/23.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVSearchShopsCell.h"
#import "BVConst.h"
#import "UIImageView+WebCache.h"
#import "BVShopsLeftCategoryDetail.h"
#import "BVShopCart.h"

@interface BVSearchShopsCell ()<UITextFieldDelegate>
// 背景
@property (weak, nonatomic) IBOutlet UIView *bgView;
// 标题
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
// 配图
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
// 内容
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
// 每袋钱数
@property (weak, nonatomic) IBOutlet UILabel *each_bag_maoneyLabel;
// 几斤装
@property (weak, nonatomic) IBOutlet UILabel *pounds_moneyLabel;
// 每斤多少钱
@property (weak, nonatomic) IBOutlet UILabel *each_poundsLabel;
//  总斤数
@property (weak, nonatomic) IBOutlet UILabel *allPoundsLabel;
// 已购多少袋
@property (weak, nonatomic) IBOutlet UILabel *allBagsLabel;


@end

@implementation BVSearchShopsCell

- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor        = [UIColor clearColor];
    self.bgView.backgroundColor = BVColor(230, 230, 230);
    self.shopsCountF.delegate   = self;
}

+ (instancetype)cell {
    return [[[NSBundle mainBundle] loadNibNamed:@"BVSearchShopsCell" owner:nil options:nil] lastObject];
}

- (void)setSearchShops:(BVShopsLeftCategoryDetail *)searchShops {
    _searchShops = searchShops;
    
    // 标题
    self.titleLable.text = searchShops.title;
    // 配图
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", Path, searchShops.pic_name]]];

    // 内容
    self.contentLabel.text = searchShops.content;

    // 每袋多少钱
    self.each_bag_maoneyLabel.text = [NSString stringWithFormat:@"%@元/%@", searchShops.each_bag_money,searchShops.each_bag_unit];

    // 几斤装
    self.pounds_moneyLabel.text = [NSString stringWithFormat:@"%@", searchShops.poundsunit];
    
    // 每斤多少钱
    self.each_poundsLabel.text  = [NSString stringWithFormat:@"每%@%@元", searchShops.each_bag_unit,searchShops.each_bag_money];
    
//    //限购数量
//    self.each_poundsLabel.text = [NSString stringWithFormat:@"剩余数量：%@",searchShops.xgCount];
//    
    BVShopsLeftCategoryDetail *shopDetail = [BVShopCart lookUpShop:searchShops];
    
    if (shopDetail != nil) {
        // 显示减少按钮
        self.subButton.hidden = NO;

        self.shopsCountF.hidden = NO;
        self.shopsCountF.text = [NSString stringWithFormat:@"%ld", (long)[BVShopCart shopCartCounts:searchShops]];
    } else {
        self.subButton.hidden = YES;
        self.shopsCountF.hidden = YES;
    }

    // 已购袋数
    self.allBagsLabel.text = [NSString stringWithFormat:@"已选%ld%@", (long)[BVShopCart shopCartCounts:searchShops],searchShops.each_bag_unit];

    // 已购多少斤
    if(searchShops.poundsunit.length !=0 ){
        self.allPoundsLabel.text = [NSString stringWithFormat:@"共%d斤", [BVShopCart shopCartCounts:searchShops] * [searchShops.poundsunit integerValue]];
    }else{
        self.allPoundsLabel.text = @"";
    }
    
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

    if (self.delegate) {
        [self.delegate bvSearchShopsCell:self shopsCountChanged:textField.text];
    }
}
@end
